package com.jsmtr.rms.service.impl;

import com.jsmtr.rms.dao.DaoService;
import com.jsmtr.rms.dao.impl.DaoServiceImpl;
import com.jsmtr.rms.entity.DoBsResultManage;
import com.jsmtr.rms.entity.PlanManage;
import com.jsmtr.rms.entity.ResponseBuilder;
import com.jsmtr.rms.service.IPlanManageService;
import net.sf.json.JSONArray;
import net.sf.json.util.JSONBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wyh
 */
@Service
public class PlanManageServiceImpl implements IPlanManageService {
    @Resource
    DaoService dao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @SuppressWarnings("unchecked")
    public String getExamList(int curPage, int pageSize, String accountName, String examRoom, String invigilator,
                              String keyWord, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        String hql = "from PlanManage where 1=1 ";
        String countHql = "select count(*) from PlanManage where 1=1";
//        String accountNameSuffix = " and accountName=:accountName ";
//        String examRoomSuffix = " and examRoom=:examRoom";
//        String invigilatorSuffix = " and invigilator=:invigilator";
//        String keyWordSuffix = " and CONCAT(COALESCE(name, ''),COALESCE(pid, ''),COALESCE(jname, ''),COALESCE(eid, '')," +
//                "COALESCE(examTime, ''),COALESCE(gid, ''),COALESCE(kid, ''),COALESCE(ename, ''),COALESCE(esname, '')) " +
//                "like :keyWord";
//        if (accountName != null && !accountName.isEmpty()) {
//            hql += accountNameSuffix;
//            countHql += accountNameSuffix;
//            params.put("accountName", accountName);
//        }
//        if (examRoom != null && !examRoom.isEmpty()) {
//            hql += examRoomSuffix;
//            countHql += examRoomSuffix;
//            params.put("examRoom", examRoom);
//        }
//        if (invigilator != null && !invigilator.isEmpty()) {
//            hql += invigilatorSuffix;
//            countHql += invigilatorSuffix;
//            params.put("invigilator", invigilator);
//        }
//        if (keyWord != null && !keyWord.isEmpty()) {
//            hql += keyWordSuffix;
//            countHql += keyWordSuffix;
//            params.put("keyWord", keyWord);
//        }
        try {
            int count = (int)(long)(Long)dao.findUnique(countHql, params);
            List<PlanManage> data = (List<PlanManage>)dao.findLimit(hql, params, (curPage - 1) * pageSize, pageSize);
            ResponseBuilder builder = new ResponseBuilder();
            return builder.setData(JSONArray.fromObject(data).toString()).setPage(curPage, count).build();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean addExam(PlanManage planManage) {
        try {
            dao.add(planManage);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean editExamTime(PlanManage planManage) {
        try {
            dao.update(planManage);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean admissionTicket(String id) {
        try {
//            PlanManage planManage = (PlanManage)dao.get(PlanManage.class, id);
//            DoBsResultManage doBsResultManage = new DoBsResultManage());
//            doBsResultManage.setPlanId(planManage.getId();
//            doBsResultManage.setKid(planManage.getKid());
//            doBsResultManage.setKcode();
//            doBsResultManage.setKname();
//            doBsResultManage.setCount();
//            doBsResultManage.setState();
//            doBsResultManage.setOperation();
//            dao.add(doBsResultManage);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean examSign(PlanManage planManage) {
        return false;
    }

    @Override
    @SuppressWarnings("unchecked")
    public String getAchievementList(int curPage, int pageSize, Map<String, ? super String> params) {
        ResponseBuilder responseBuilder = new ResponseBuilder();
        try {
            String hql = "from DoBsResultManage where 1=1";
            String countHql = "select count(*) from DoBsResultManage where 1=1";
            int count = (int)(long)(Long)dao.findUnique(countHql);
            List<DoBsResultManage> bsResList = dao.findLimit(hql, (curPage - 1) * pageSize, pageSize);
            String data = JSONArray.fromObject(bsResList).toString();
            return responseBuilder.setPage(curPage, count).setData(data).build();
        } catch (Exception e) {
            e.printStackTrace();
            return responseBuilder.setStatus(ResponseBuilder.ERROR).setMsg(ResponseBuilder.ERROR_STR).build();
        }
    }

    @Override
    public boolean importAchievement() {
        return false;
    }

    public void setDao(DaoService dao) {
        this.dao = dao;
    }
}
